package com.any.hook.core.webapi.api;

import com.any.hook.core.webapi.entity.DBInfo;
import com.any.hook.core.webapi.entity.Result;
import com.any.hook.core.webapi.service.EtlDBService;
import com.any.hook.core.webapi.util.LoadDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;

@Controller
public class EtlDBController {
    @Autowired
    private EtlDBService etlDBService;
    @PostMapping("/db/check/url")
    @ResponseBody
    public Result checkDbUrl(@RequestBody DBInfo dbInfo) {
        LoadDriver.loadDriver(dbInfo.getDriverUrl());
        try (Connection connection = DriverManager.getConnection(DBInfo.getFullDriverUrl(dbInfo), dbInfo.getUser(), dbInfo.getPassword());) {
            if (connection != null && connection.isValid(1000)) {
                return Result.SUSSRESULT();
            } else {
                return Result.ERRORRESULT("数据库连接是无效的！请重新配置");
            }
        } catch (Exception e) {
            return Result.ERRORRESULT(e.getMessage());
        } finally {

        }
    }

    @GetMapping("/db/find/{name}")
    @ResponseBody
    public Result findName(@PathVariable String name) {
        if (name != null && name.length() > 0) {
            return Result.SUSSRESULT(new Integer(etlDBService.findName(name)));
        } else {
            return Result.ERRORRESULT("参数无效，请检查参数！");
        }
    }

    @GetMapping("/db/findby/{name}")
    @ResponseBody
    public Result findByName(@PathVariable String name) {
        if (name != null && name.length() > 0) {
            DBInfo dbInfo = etlDBService.findByName(name);
            if (dbInfo != null && dbInfo.getName() != null && dbInfo.getName().length() > 0) {
                dbInfo.setFullJDBCURL(DBInfo.getFullDriverUrl(dbInfo));
                return Result.SUSSRESULT(dbInfo);
            }
            return Result.ERRORRESULT("未发现以此命名的数据库信息");
        } else {
            return Result.ERRORRESULT("参数无效，请检查参数！");
        }
    }

    @PostMapping("/db/info/save")
    @ResponseBody
    public Result saveDbUrl(@RequestBody DBInfo dbInfo) {
        String name=dbInfo.getName();
        if(name == null && name.length()<1){
            return Result.ERRORRESULT("提交的信息内容丢失，请核对提交信息！");
        }
        int count = etlDBService.saveDBInfo(dbInfo);
        if (count == 1) {
            return Result.SUSSRESULT(null, "数据库信息保存成功！");
        } else {
            return Result.ERRORRESULT("数据库信息保存失败！");
        }
    }

    @GetMapping("/db/deleteby/{name}")
    @ResponseBody
    public Result deleteByName(@PathVariable String name) {
        if (name != null && name.length() > 0) {
            int tag = etlDBService.deleteByName(name);
            if (tag > 0) {
                return Result.SUSSRESULT(new Integer(tag));
            }
            return Result.ERRORRESULT("未发现可以删除的数据库信息");
        } else {
            return Result.ERRORRESULT("参数无效，请检查参数！");
        }
    }

    @GetMapping("/db/check/getall/{page}/{size}")
    @ResponseBody
    public Result getAllDBInfo(@PathVariable int page, @PathVariable int size) {
        if( page < 1 || size < 1){
            return Result.ERRORRESULT("指定的参数错误，请核对参数！");
        }
        return Result.SUSSRESULT(etlDBService.getAllDBInfo(page,size));
    }

    @PostMapping("/db/update")
    @ResponseBody
    public Result updateDBInfo(@RequestBody DBInfo dbInfo) {
        String name=dbInfo.getName();
        if(name == null && name.length()<1){
            return Result.ERRORRESULT("提交的信息内容丢失，请核对提交信息！");
        }
        int count = etlDBService.updateByName(dbInfo);
        if (count == 1) {
            return Result.SUSSRESULT(null, "数据库信息更新成功！");
        } else {
            return Result.ERRORRESULT("数据库信息更新失败！");
        }
    }

    @GetMapping("/db/dbinfo/count")
    @ResponseBody
    public Result getDBInfoCount(){
        return Result.SUSSRESULT(new Integer(etlDBService.selectCount()));
    }

}
